package com.probox.common.entity.service.wechat.impl;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.probox.common.core.enums.DataStatus;
import com.probox.common.core.enums.OnSaleEnum;
import com.probox.common.core.enums.WechatLiveStatus;
import com.probox.common.core.exception.BaseException;
import com.probox.common.core.utils.SecurityUtils;
import com.probox.common.entity.domain.wechat.WechatAuthorizationInfo;
import com.probox.common.entity.domain.wechat.broadcast.*;
import com.probox.common.entity.domain.wechat.vo.AddGoodsVo;
import com.probox.common.entity.domain.wechat.vo.QueryRoomVo;
import com.probox.common.entity.domain.wechat.vo.ResponseWechatBroadcastVo;
import com.probox.common.entity.mapper.wechat.WchatBroadcastMapper;
import com.probox.common.entity.mapper.wechat.WechatBroadcastProductMapper;
import com.probox.common.entity.mapper.wechat.WechatBroadcastProductMiddleMapper;
import com.probox.common.entity.service.wechat.IWchatBroadcastService;
import com.probox.common.entity.service.wechat.WechatBroadcastService;
import com.probox.common.entity.service.wechat.WechatMiniService;
import com.probox.common.entity.utils.HttpUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Kite
 * @date 2021/7/9
 */
@Service
@Transactional
public class WechatBroadcastServiceImpl implements WechatBroadcastService {

    @Autowired
    private WechatMiniService wechatMiniService;

    @Autowired
    private WechatBroadcastProductMapper wechatBroadcastProductMapper;

    @Autowired
    private WchatBroadcastMapper wchatBroadcastMapper;

    private final String applet = "applet";

    private final String errcode = "0";

    private final String empty = "9410000";

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private IWchatBroadcastService wchatBroadcastService;

    @Autowired
    private WechatBroadcastProductMiddleServiceImpl wechatBroadcastProductMiddleService;
    @Autowired
    private WechatBroadcastProductMiddleMapper wechatBroadcastProductMiddleMapper;


    //申请直播权限
    private final String  APPLY_FOR = "https://api.weixin.qq.com/wxa/business/applyliveinfo?access_token=";

    //创建直播间
    private final String CREATE_ROOM_URL = "https://api.weixin.qq.com/wxaapi/broadcast/room/create?access_token=";

    //获取直播间列表
    private final String LIVEINFO_URL = "https://api.weixin.qq.com/wxa/business/getliveinfo?access_token=";

    //直播间导入商品
    private final String ADDGOODS_URL = "https://api.weixin.qq.com/wxaapi/broadcast/room/addgoods?access_token=";

    //删除直播间
    private final String REMOVEASSISTANT_URL = "https://api.weixin.qq.com/wxaapi/broadcast/room/deleteroom?access_token=";

    //编辑直播间
    private final String EDITROOM_URL = "https://api.weixin.qq.com/wxaapi/broadcast/room/editroom?access_token=";

    //获取直播间分享二维码
    private final String SHAREDCODE_URL = "https://api.weixin.qq.com/wxaapi/broadcast/room/getsharedcode?access_token=";

    //上下架商品
    private final String ONSALE_URL = "https://api.weixin.qq.com/wxaapi/broadcast/goods/onsale?access_token=";

    //删除商品
    private final String DELETEIN_ROOM_URL = "https://api.weixin.qq.com/wxaapi/broadcast/goods/deleteInRoom?access_token=";


    /**
     * 申请直播间权限
     * @return
     */
    @Override
    public Boolean applyFor(){
        WechatAuthorizationInfo authorizerAppid = wechatMiniService.getAuthorizerAppid(applet);
        String url = APPLY_FOR + authorizerAppid.getAuthorizerAccessToken();
        JSONObject json = new JSONObject();
        json.put("action", "apply");
        logger.info("申请开通直播权限请求参数：{}", json);
        String data = HttpUtil.PostUrl(url, json);
        logger.info("查询申请开通直播权限结果：{}", data);
        json = JSONObject.parseObject(data);
        if (!errcode.equals(json.getString("errcode"))){
            throw new BaseException(json.getString("errmsg"));
        }
        return true;
    }


    /**
     * 查询直播权限
     * @return
     */
    @Override
    public String queryPermissions() {
        WechatAuthorizationInfo authorizerAppid = wechatMiniService.getAuthorizerAppid(applet);
        RestTemplate restTemplate = new RestTemplate();
        String url = LIVEINFO_URL + authorizerAppid.getAuthorizerAccessToken();
        ResponseEntity<LiveRooms> responseEntity;
        JSONObject json = new JSONObject();
        json.put("start", 0);
        json.put("limit",1);
        logger.info("查询权限请求参数：{}", json);
        responseEntity = restTemplate.postForEntity(url, json, LiveRooms.class);
        LiveRooms body = responseEntity.getBody();
        logger.info("查询权限结果：{}", body.toString());
        String e = body.getErrcode().toString();
        if (empty.equals(e)){
            e = "0";
        }
        return e;
    }


    /**
     * 创建直播间
     * @return
     */
    @Override
    public boolean creatRoom(WechatBroadcast wechatBroadcast) {
        WechatAuthorizationInfo authorizerAppid = wechatMiniService.getAuthorizerAppid(applet);
        String url = CREATE_ROOM_URL + authorizerAppid.getAuthorizerAccessToken();

        wchatBroadcastService.insertWchatBroadcast(wechatBroadcast);

        Broadcast broadcast = new Broadcast();
        BeanUtils.copyProperties(wechatBroadcast,broadcast);
        broadcast.setType(0L);
        broadcast.setCloseLike(0L);
        broadcast.setCloseGoods(0L);
        broadcast.setCloseComment(0L);
        broadcast.setCloseReplay(0L);
        String string = JSONObject.toJSONString(broadcast);
        JSONObject jsonObject = JSONObject.parseObject(string);
        jsonObject.remove("id");
        Long startTime = jsonObject.getLong("startTime");
        Long endTime = jsonObject.getLong("endTime");
        jsonObject.put("startTime",startTime/1000);
        jsonObject.put("endTime",endTime/1000);
        logger.info("创建直播间请求参数：{}",jsonObject);
        String data = HttpUtil.PostUrl(url, jsonObject);
        logger.info("创建直播间结果：{}",data);
        jsonObject = JSONObject.parseObject(data);
        String errcode = jsonObject.getString("errcode");
        boolean msg = this.errcode.equals(errcode);
        if (msg){
            wechatBroadcast.setRoomId(jsonObject.getLong("roomId"));
            wchatBroadcastService.updateWchatBroadcast(wechatBroadcast);
        }else {
            List<HashMap> hashMaps = wchatBroadcastMapper.selectDictData("wechart_errcode_code", errcode);
            throw new BaseException(hashMaps.get(0).get("dict_label").toString());
        }
        return msg;
    }

    /**
     * 查询直播间列表
     *
     * @return
     */
    @Override
    public LiveRooms getRoomList(QueryRoomVo queryRoomVo) {
        WechatAuthorizationInfo authorizerAppid = wechatMiniService.getAuthorizerAppid(applet);
        RestTemplate restTemplate = new RestTemplate();
        String url = LIVEINFO_URL + authorizerAppid.getAuthorizerAccessToken();
        ResponseEntity<LiveRooms> responseEntity;
        JSONObject json = new JSONObject();
        json.put("start", (queryRoomVo.getPage() - 1) * queryRoomVo.getPagelimit());
        json.put("limit", queryRoomVo.getPagelimit());
        logger.info("查询直播间列表请求参数：{}",json);
        responseEntity = restTemplate.postForEntity(url, json, LiveRooms.class);
        LiveRooms body = responseEntity.getBody();
        logger.info("查询直播间列表结果：{}",body.toString());

        if (body!=null){

            SimpleDateFormat month = new SimpleDateFormat("MM月dd日");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat hour = new SimpleDateFormat("HH:mm");
            SimpleDateFormat sky = new SimpleDateFormat("MM月dd日 HH:mm");

            SimpleDateFormat skyWhole = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");

            List<LiveRoomInfo> roomInfo = body.getRoom_info();
            roomInfo = roomInfo.stream().map(o -> {

                LiveRoomInfo liveRoomInfo = new LiveRoomInfo();
                BeanUtils.copyProperties(o, liveRoomInfo);

                liveRoomInfo.setCoverImgUrl(liveRoomInfo.getCover_img());
                liveRoomInfo.setFeedsImgUrl(liveRoomInfo.getFeeds_img());
                liveRoomInfo.setShareImgUrl(liveRoomInfo.getShare_img());

                //时间转换
                long startTimestamp = liveRoomInfo.getStart_time();
                long endTimestamp = liveRoomInfo.getEnd_time();

                liveRoomInfo.setDate(month.format(new Date( startTimestamp* 1000L)));
                if (sdf.format(new Date(startTimestamp * 1000L)).equals(sdf.format(new Date(endTimestamp * 1000L)))){
                    StringBuffer time = new StringBuffer(hour.format(new Date(startTimestamp * 1000L)));
                    time.append("-");
                    time.append(hour.format(new Date(endTimestamp * 1000L)));
                    liveRoomInfo.setTime(time.toString());
                }else{
                    StringBuffer time = new StringBuffer(hour.format(new Date(startTimestamp * 1000L)));
                    time.append("-");
                    time.append(sky.format(new Date(endTimestamp * 1000L)));
                    liveRoomInfo.setTime(time.toString());
                }
                liveRoomInfo.setStartTimeStr(skyWhole.format(new Date(startTimestamp * 1000L)));
                liveRoomInfo.setEndTimeStr(skyWhole.format(new Date(endTimestamp * 1000L)));

                QueryWrapper<WechatBroadcast> wechatBroadcastQueryWrapper = new QueryWrapper<>();
                wechatBroadcastQueryWrapper.eq("PLATFORM_UID",SecurityUtils.getPlatformId());
                wechatBroadcastQueryWrapper.eq("ROOM_ID",o.getRoomid());
                WechatBroadcast wechatBroadcast = wchatBroadcastMapper.selectOne(wechatBroadcastQueryWrapper);

                if (wechatBroadcast!=null){

                    liveRoomInfo.setAnchorWechat(wechatBroadcast.getAnchorWechat());
                    liveRoomInfo.setCoverImgStr(wechatBroadcast.getCoverImgStr());
                    liveRoomInfo.setShareImgStr(wechatBroadcast.getShareImgStr());
                    liveRoomInfo.setFeedsImgStr(wechatBroadcast.getFeedsImgStr());
                    liveRoomInfo.setCover_img(wechatBroadcast.getCoverImg());
                    liveRoomInfo.setShare_img(wechatBroadcast.getShareImg());
                    liveRoomInfo.setFeeds_img(wechatBroadcast.getFeedsImg());

                    List<Good> goods = o.getGoods();
                    ArrayList<Good> objects = new ArrayList<>();
                    for (int i = 0; i <goods.size() ; i++) {
                        QueryWrapper<WechatBroadcastProductMiddle> wechatBroadcastProductMiddleQueryWrapper = new QueryWrapper<>();
                        wechatBroadcastProductMiddleQueryWrapper.eq("ROOM_ID",o.getRoomid());
                        wechatBroadcastProductMiddleQueryWrapper.eq("GOODS_ID",goods.get(i).getGoods_id());
                        wechatBroadcastProductMiddleQueryWrapper.eq("DATA_STATUS",DataStatus.valid.getKey());
                        WechatBroadcastProductMiddle wechatBroadcastProductMiddle = wechatBroadcastProductMiddleMapper.selectOne(wechatBroadcastProductMiddleQueryWrapper);
                        logger.info("查询直播间和商品关系结果：{}",wechatBroadcastProductMiddle);

                        Good good = new Good();
                        BeanUtils.copyProperties(goods.get(i),good);
                        if (wechatBroadcastProductMiddle == null ||  wechatBroadcastProductMiddle.getId() == null){
                            wechatBroadcastProductMiddle = new WechatBroadcastProductMiddle();

                            QueryWrapper<WechatBroadcastProduct> queryWrapper = new QueryWrapper<>();
                            queryWrapper.eq("PLATFORM_UID",SecurityUtils.getPlatformId());
                            queryWrapper.eq("GOODS_ID",goods.get(i).getGoods_id());
                            logger.info("查询列表商品直播间ID");
                            WechatBroadcastProduct wechatBroadcastProduct = wechatBroadcastProductMapper.selectOne(queryWrapper);
                            if (wechatBroadcastProduct != null){
                                wechatBroadcastProductMiddle.setProductId(wechatBroadcastProduct.getProductId());
                                wechatBroadcastProductMiddle.setRoomId(new Long(o.getRoomid()));
                                wechatBroadcastProductMiddle.setGoodsId(new Long(goods.get(i).getGoods_id() ));
                                wechatBroadcastProductMiddle.setDataStatus(new Long(DataStatus.valid.getKey()));
                                wechatBroadcastProductMiddle.setOnSale(OnSaleEnum.putaway.getKey());
                                wechatBroadcastProductMiddle.setPlatformUid(SecurityUtils.getPlatformId());
                                wechatBroadcastProductMiddleService.save(wechatBroadcastProductMiddle);
                                good.setOn_sale(OnSaleEnum.putaway.getKey());
                            }
                        }else {
                            good.setOn_sale(wechatBroadcastProductMiddle.getOnSale());
                        }
                        objects.add(good);
                    }
                    liveRoomInfo.setGoods(objects);
                    liveRoomInfo.setJurisdiction(1);
                }else {
                    liveRoomInfo.setJurisdiction(0);
                }


                return liveRoomInfo;
            }).collect(Collectors.toList());
            body.setRoom_info(roomInfo);
        }

        return body;
    }

    /**
     * 删除直播间
     * @return
     */
    @Override
    public boolean deleteRoom(Integer id) {
        UpdateWrapper<WechatBroadcast> wchatBroadcastQueryWrapper = new UpdateWrapper<>();
        wchatBroadcastQueryWrapper.eq("PLATFORM_UID", SecurityUtils.getPlatformId());
        wchatBroadcastQueryWrapper.eq("ROOM_ID",id);
        wchatBroadcastQueryWrapper.set("DATA_STATUS",DataStatus.delete.getKey());
        boolean update = wchatBroadcastService.update(wchatBroadcastQueryWrapper);
        logger.info("删除直播间结果：{}",update);

        WechatAuthorizationInfo authorizerAppid = wechatMiniService.getAuthorizerAppid(applet);
        String url = REMOVEASSISTANT_URL + authorizerAppid.getAuthorizerAccessToken();
        JSONObject json = new JSONObject();
        json.put("id",id);
        logger.info("删除直播间请求删除：{}",json);
        String data = HttpUtil.PostUrl(url, json);
        logger.info("删除直播间结果：{}",data);
        json = JSONObject.parseObject(data);
        String errcode = json.getString("errcode");
        boolean msg = this.errcode.equals(errcode);
        if (!msg){
            List<HashMap> hashMaps = wchatBroadcastMapper.selectDictData("wechart_errcode_code", errcode);
            throw new BaseException(hashMaps.get(0).get("dict_label").toString());
        }
        return msg;
    }


    /**
     * 新增直播间商品
     * @return
     */
    @Override
    public boolean addGoods(AddGoodsVo addGoodsVo){
        WechatAuthorizationInfo authorizerAppid = wechatMiniService.getAuthorizerAppid(applet);
        String url = ADDGOODS_URL +  authorizerAppid.getAuthorizerAccessToken();

        ArrayList<WechatBroadcastProductMiddle> wechatBroadcastProductMiddles = new ArrayList<>();
        for (int i = 0; i <addGoodsVo.getIds().length ; i++) {
            WechatBroadcastProductMiddle wechatBroadcastProductMiddle = new WechatBroadcastProductMiddle();
            QueryWrapper<WechatBroadcastProduct> wechatBroadcastProductQueryWrapper = new QueryWrapper<>();
            wechatBroadcastProductQueryWrapper.eq("GOODS_ID",addGoodsVo.getIds()[i]);
            WechatBroadcastProduct wechatBroadcastProduct = wechatBroadcastProductMapper.selectOne(wechatBroadcastProductQueryWrapper);

            QueryWrapper<WechatBroadcastProductMiddle> queryWrapper = new QueryWrapper<WechatBroadcastProductMiddle>();
            queryWrapper.eq("PLATFORM_UID",SecurityUtils.getPlatformId());
            queryWrapper.eq("ROOM_ID",addGoodsVo.getRoomId());
            queryWrapper.eq("GOODS_ID",addGoodsVo.getIds()[i]);
            Integer integer = wechatBroadcastProductMiddleMapper.selectCount(queryWrapper);
            if (integer == 0){
                wechatBroadcastProductMiddle.setProductId(wechatBroadcastProduct.getProductId());
                wechatBroadcastProductMiddle.setPlatformUid(SecurityUtils.getPlatformId());
                wechatBroadcastProductMiddle.setGoodsId(Long.parseLong(addGoodsVo.getIds()[i]));
                wechatBroadcastProductMiddle.setRoomId(Long.parseLong(addGoodsVo.getRoomId()));
                wechatBroadcastProductMiddles.add(wechatBroadcastProductMiddle);
            }
        }

        wechatBroadcastProductMiddleService.saveBatch(wechatBroadcastProductMiddles);


        String res = JSONObject.toJSONString(addGoodsVo);
        JSONObject jsonObject = JSONObject.parseObject(res);
        jsonObject.remove("id");
        logger.info("新增直播间商品请求参数：{}",jsonObject);
        String data = HttpUtil.PostUrl(url, jsonObject);
        logger.info("新增直播间商品结果：{}",data);
        jsonObject = JSONObject.parseObject(data);
        boolean errcode = this.errcode.equals(jsonObject.getString("errcode"));
        if (!errcode){
            List<HashMap> hashMaps = wchatBroadcastMapper.selectDictData("wechart_errcode_code", jsonObject.getString("errcode"));
            throw new BaseException(hashMaps.get(0).get("dict_label").toString());
        }
        return errcode;
    }

    /**
     * 编辑直播间
     * @return
     */
    @Override
    public boolean updateRoom(WechatBroadcast wechatBroadcast) {
        WechatAuthorizationInfo authorizerAppid = wechatMiniService.getAuthorizerAppid(applet);
        String url = EDITROOM_URL + authorizerAppid.getAuthorizerAccessToken();

        wchatBroadcastService.updateWchatBroadcast(wechatBroadcast);

        Broadcast broadcast = new Broadcast();
        BeanUtils.copyProperties(wechatBroadcast,broadcast);

        broadcast.setType(0L);
        broadcast.setCloseLike(0L);
        broadcast.setCloseGoods(0L);
        broadcast.setCloseComment(0L);
        broadcast.setCloseReplay(0L);

        broadcast.setId(wechatBroadcast.getRoomId());
        String string = JSONObject.toJSONString(broadcast);
        JSONObject jsonObject = JSONObject.parseObject(string);

        Long startTime = jsonObject.getLong("startTime");
        Long endTime = jsonObject.getLong("endTime");
        jsonObject.put("startTime",startTime/1000);
        jsonObject.put("endTime",endTime/1000);

        logger.info("编辑直播间请求参数：{}",jsonObject);
        String spRes = HttpUtil.PostUrl(url, jsonObject);
        logger.info("编辑直播间结果：{}",spRes);
        jsonObject = JSONObject.parseObject(spRes);

        boolean msg = this.errcode.equals(jsonObject.getString("errcode"));
        if (msg){
            wechatBroadcast.setRoomId(jsonObject.getLong("roomId"));
            wchatBroadcastService.updateWchatBroadcast(wechatBroadcast);
        }else {
            List<HashMap> hashMaps = wchatBroadcastMapper.selectDictData("wechart_errcode_code", jsonObject.getString("errcode"));
            throw new BaseException(hashMaps.get(0).get("dict_label").toString());
        }
        return msg;
    }

    /**
     * 获取直播间二维码
     * @return
     */
    @Override
    public BroadcastShare getShare(Integer roomId) {
        WechatAuthorizationInfo authorizerAppid = wechatMiniService.getAuthorizerAppid(applet);
        RestTemplate restTemplate = new RestTemplate();
        String url = SHAREDCODE_URL + authorizerAppid.getAuthorizerAccessToken()+"&roomId="+roomId;
        logger.info("获取直播间二维码请求参数：{}",url);
        ResponseEntity<BroadcastShare> broadcastShareResponseEntity = restTemplate.getForEntity(url, BroadcastShare.class);
        BroadcastShare body = broadcastShareResponseEntity.getBody();
        logger.info("获取直播间二维码返回结果：{}",body);
        return body;
    }

    /**
     * 上下架商品
     * @return
     */
    @Override
    public boolean onsale(QueryRoomVo queryRoomVo) {
        WechatAuthorizationInfo authorizerAppid = wechatMiniService.getAuthorizerAppid(applet);
        String url = ONSALE_URL + authorizerAppid.getAuthorizerAccessToken();

        UpdateWrapper<WechatBroadcastProductMiddle> updateWrapper = new UpdateWrapper<WechatBroadcastProductMiddle>();
        updateWrapper.set("ON_SALE",queryRoomVo.getOnSale());
        updateWrapper.eq("ROOM_ID",queryRoomVo.getId());
        updateWrapper.eq("PLATFORM_UID", SecurityUtils.getPlatformId());
        updateWrapper.eq("GOODS_ID", queryRoomVo.getGoodsId());
        wechatBroadcastProductMiddleService.update(updateWrapper);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("roomId",queryRoomVo.getId());
        jsonObject.put("goodsId",queryRoomVo.getGoodsId());
        jsonObject.put("onSale",queryRoomVo.getOnSale());
        logger.info("上下架商品请求参数：{}",jsonObject);
        String spRes = HttpUtil.PostUrl(url, jsonObject);
        logger.info("上下架商品结果：{}",spRes);
        jsonObject = JSONObject.parseObject(spRes);
        return errcode.equals(jsonObject.getString("errcode"));
    }


    /**
     * 删除直播间商品
     * @return
     */
    @Override
    public boolean deleteRoomGood(QueryRoomVo queryRoomVo) {
        WechatAuthorizationInfo authorizerAppid = wechatMiniService.getAuthorizerAppid(applet);
        String url = DELETEIN_ROOM_URL + authorizerAppid.getAuthorizerAccessToken();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("roomId",queryRoomVo.getId());
        jsonObject.put("goodsId",queryRoomVo.getGoodsId());
        logger.info("删除直播间商品请求参数：{}",jsonObject);
        String spRes = HttpUtil.PostUrl(url, jsonObject);
        logger.info("删除直播间商品结果：{}",spRes);
        jsonObject = JSONObject.parseObject(spRes);
        return errcode.equals(jsonObject.getString("errcode"));
    }

    /**
     * 查询数据库直播间列表
     * @param name
     * @return
     */
    @Override
    public List<ResponseWechatBroadcastVo> getList(String name) {
        QueryWrapper<WechatBroadcast> queryWrapper = Wrappers.query();
        if (!StringUtils.isEmpty(name)){
            queryWrapper.like("name", name);
        }
        queryWrapper.eq("DATA_STATUS",DataStatus.valid.getKey());
        queryWrapper.eq("PLATFORM_UID",SecurityUtils.getPlatformId());
        List<WechatBroadcast> wechatBroadcasts = wchatBroadcastMapper.selectList(queryWrapper);
        List<ResponseWechatBroadcastVo> collect = wechatBroadcasts.stream().map(o -> {
            ResponseWechatBroadcastVo vo = new ResponseWechatBroadcastVo();
            BeanUtils.copyProperties(o,vo);
            Date date = DateUtil.date();
            if(date.before(o.getStartTime())){
                vo.setLiveStatus(WechatLiveStatus.no_started.getKey());
                vo.setLiveStatusStr(WechatLiveStatus.no_started.getValue());
            }else if(date.after(o.getEndTime())){
                vo.setLiveStatus(WechatLiveStatus.past.getKey());
                vo.setLiveStatusStr(WechatLiveStatus.past.getValue());
            }else {
                vo.setLiveStatus(WechatLiveStatus.finished.getKey());
                vo.setLiveStatusStr(WechatLiveStatus.finished.getValue());
            }
            return vo;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public Integer getWchatBroadcastTotal(String name) {
        QueryWrapper<WechatBroadcast> queryWrapper = Wrappers.query();
        if (com.probox.common.core.utils.StringUtils.isNotEmpty(String.valueOf(name)) && name != null) {
            queryWrapper.like("name", name);
        }
        queryWrapper.ne("DATA_STATUS",DataStatus.delete.getKey());
        queryWrapper.eq("PLATFORM_UID",SecurityUtils.getPlatformId());
        return wchatBroadcastMapper.selectCount(queryWrapper);
    }


}
